<?php

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Model\Repair;
use Illuminate\Http\Request;
use RepairService;
use AdminService;
use Carbon\Carbon;
use RepairTransformer;
class RepairController extends Controller
{
    protected $transformer;

    public function __construct(RepairTransformer $transformer)
    {
        $this->transformer = $transformer;
    }

    public function checkMinister(Request $request)
    {
        $admin = $request->user();

        $minister_Authorization = RepairService::checkMinister($admin);

        return response()->json(compact('minister_Authorization'));
    }

    public function getRepair()
    {
        $repairOrders = RepairService::getRepair();

        $data = RepairTransformer::order($repairOrders->toArray());

        return response()->json($data);
    }

    public function assignRepair(Request $request)
    {
        $admin = $request->user();
        $id = $request->input('id');

        $result = RepairService::assignRepair($admin, $id);

        return response()->json(compact('result'));
    }

    public function repairTask(Request $request)
    {
        $admin = $request->user();

        $repairTasks = RepairService::repairTask($admin);

        $data = RepairTransformer::order($repairTasks->toArray());


        return response()->json($data);
    }

    public function finishRepair(Request $request)
    {
        $admin = $request->user();
        $id = $request->input('id');

        $result = RepairService::updateRepair($admin, $id);

        return response()->json(compact('result'));
    }

    public function cancelRepair(Request $request)
    {
        $admin = $request->user();
        $id = $request->input('id');

        $result = RepairService::cancelRepair($admin, $id);

        return response()->json(compact('result'));
    }

    public function getOfficer(Request $request)
    {
        $admin = $request->user();
        
        $officers = AdminService::getOfficer($admin);

        $data = RepairTransformer::operator($officers->toArray());

        return response()->json($data);
    }

    public function setRepair(Request $request)
    {
        $admin = $request->user();
        $officerId = $request->input('officer_id');
        $repairId = $request->input('id');
        $result = RepairService::setRepair($admin, $officerId, $repairId);

        return response()->json(compact('result'));
    }

    public function getFinishOrder(Request $request)
    {
        $admin = $request->user();

        $finishOrder = RepairService::getFinishOrder($admin);

        $data = RepairTransformer::order($finishOrder->toArray());

        return response()->json($data);
    }
    
    public function getTask()
    {
        $allTask = RepairService::getTask();

        $data = RepairTransformer::task($allTask->toArray());

        return response()->json($data);
    }
    
    public function rank(Request $request)
    {
        $admins = AdminService::getAdminByName($request->input('role'));
        $data = RepairTransformer::rank($admins->toArray());

        return response()->json($data);
    }
}
